library(magrittr)
library(plyr)
library(tidyverse)
library(readxl)
library(vietnamcode)
library(car)


rm(list=ls())
home = 'C:/Users/jdt34/Dropbox/VNA_Responsiveness/Short Article/JOP-dataverse/'


dv_survey = paste0(home, 'Data/survey-outcomes.xlsx') %>%
  read_xlsx %>%
  mutate(Treatment=factor(x=Treatment, 
                          levels=c('Control',
                                   'Citizen',
                                   'Firm')),
         Missing=as.integer(is.na(Q1))) %>%
  subset(!is.na(Treatment))


dv_pooled = paste0(home, 'Data/pooled-outcomes.xlsx') %>%
  read_xlsx %>%
  mutate(Treatment=factor(x=Treatment, 
                          levels=c('Control',
                                   'Citizen',
                                   'Firm'))) %>%
  subset(!is.na(Treatment)) %>%
  arrange(Province, Name_VN)


df_delegate_raw = paste0(home, 'Data/14th-VNA-membership.csv') %>%
  read_csv %>%
  mutate(Province=mapvalues(x=province, 
                            from=vietnamcode_data$province_name_diacritics, 
                            to=vietnamcode_data$province_name,
                            warn_missing=F)) %>%
  mutate(Province=as.character(Province))
df_delegate_raw$Province[str_detect(string=df_delegate_raw$province, 
                                    pattern='a - V')] = 'BRVT'
df_delegate_raw = arrange(.data=df_delegate_raw, Province, name)
df_delegate_raw$ID = llply(.data=df_delegate_raw$name, 
                           .fun=function(x) {
  found = which(str_detect(string=dv_pooled$Name_VN, pattern=x))
  if(length(found)<1) {
    NA
  } else if(length(found)==1) {
    dv_pooled$ID[found]
  } else {
    999L
  }
}) %>% unlist
df_delegate_raw$ID[is.na(df_delegate_raw$ID) | df_delegate_raw$ID==999L] = as.integer(c(59,57,157,257,149,201,233,134,133,455,449,115,262,260,197,120,33,369,215,88,298,174,312,422,418,419,69,393,137,221,109,253,NA,22,404,92,9,234,236,19,331,163,514,302,358,81))


df_delegate_raw_survey = merge(x=df_delegate_raw, 
                               y=subset(dv_survey, 
                                        select=-c(Province,
                                                  Dosage)), 
                               by='ID', 
                               all.x=T) %>%
  subset(!is.na(ID))
df_delegate_raw_survey$Prop.Treated = df_delegate_raw_survey$Prop.Citizen + df_delegate_raw_survey$Prop.Firm
df_delegate_raw_survey$Dosage = 0.5
df_delegate_raw_survey$Dosage[df_delegate_raw_survey$Prop.Treated==0] = 0
df_delegate_raw_survey$Dosage[df_delegate_raw_survey$Prop.Treated==1] = 1


df_delegate_raw_survey$Bucket = '000 Control'
df_delegate_raw_survey$Bucket[df_delegate_raw_survey$Treatment=='Control' & df_delegate_raw_survey$Dosage==0.5] = '050 Control'
df_delegate_raw_survey$Bucket[df_delegate_raw_survey$Treatment=='Citizen' & df_delegate_raw_survey$Dosage==0.5] = '050 Citizen'
df_delegate_raw_survey$Bucket[df_delegate_raw_survey$Treatment=='Citizen' & df_delegate_raw_survey$Dosage==1] = '100 Citizen'
df_delegate_raw_survey$Bucket[df_delegate_raw_survey$Treatment=='Firm' & df_delegate_raw_survey$Dosage==0.5] = '050 Firm'
df_delegate_raw_survey$Bucket[df_delegate_raw_survey$Treatment=='Firm' & df_delegate_raw_survey$Dosage==1] = '100 Firm'
df_delegate_raw_survey$Bucket = factor(x=df_delegate_raw_survey$Bucket, 
                                       levels=c('000 Control','050 Control','050 Citizen','100 Citizen','050 Firm','100 Firm'))


df_delegate_raw_pooled = merge(x=df_delegate_raw, 
                               y=subset(dv_pooled, 
                                        select=-c(Province,
                                                  Dosage)), 
                               by='ID', 
                               all.x=T) %>%
  subset(!is.na(ID))
df_delegate_raw_pooled$Prop.Treated = df_delegate_raw_pooled$Prop.Citizen + df_delegate_raw_pooled$Prop.Firm
df_delegate_raw_pooled$Dosage = 0.5
df_delegate_raw_pooled$Dosage[df_delegate_raw_pooled$Prop.Treated==0] = 0
df_delegate_raw_pooled$Dosage[df_delegate_raw_pooled$Prop.Treated==1] = 1


df_delegate_raw_pooled$Bucket = '000 Control'
df_delegate_raw_pooled$Bucket[df_delegate_raw_pooled$Treatment=='Control' & df_delegate_raw_pooled$Dosage==0.5] = '050 Control'
df_delegate_raw_pooled$Bucket[df_delegate_raw_pooled$Treatment=='Citizen' & df_delegate_raw_pooled$Dosage==0.5] = '050 Citizen'
df_delegate_raw_pooled$Bucket[df_delegate_raw_pooled$Treatment=='Citizen' & df_delegate_raw_pooled$Dosage==1] = '100 Citizen'
df_delegate_raw_pooled$Bucket[df_delegate_raw_pooled$Treatment=='Firm' & df_delegate_raw_pooled$Dosage==0.5] = '050 Firm'
df_delegate_raw_pooled$Bucket[df_delegate_raw_pooled$Treatment=='Firm' & df_delegate_raw_pooled$Dosage==1] = '100 Firm'
df_delegate_raw_pooled$Bucket = factor(x=df_delegate_raw_pooled$Bucket, 
                                       levels=c('000 Control','050 Control','050 Citizen','100 Citizen','050 Firm','100 Firm'))


fit_1 = lm(Q1 ~ 0 + Bucket, data=df_delegate_raw_survey)
fit_2 = lm(Spoke ~ 0 + Bucket, data=df_delegate_raw_pooled)


stargazer(fit_1, 
          fit_2,
          df=F,
          ci=T,
          title='Examining results by provincial saturation.', 
          covariate.labels=c('Control (0% Saturation)',
                             'Control (50% Saturation)',
                             'Citizen (50% Saturation)',
                             'Citizen (100% Saturation)',
                             'Firm (50% Saturation)',
                             'Firm (100% Saturation)'),
          align=T, 
          header=F,
          no.space=T, 
          star.cutoffs=NA,
          column.labels=c('Prepared','Spoke'),
          dep.var.labels.include=F,
          label='tab:saturation',
          notes.append=F, 
          notes='Linear regressions with suppressed intercepts. Coefficient 95% confidence intervals displayed in parentheses.') %>%
  writeClipboard

